package com.easycarebn.main.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

import com.easycarebn.main.utils.IResult;
import com.easycarebn.main.utils.Utils;
import com.easycarebn.request.controller.ServerController;
import com.easycarebn.request.data.DataItemResponse;
import com.easycarebn.request.data.UserResponse;
import com.easycarebn.service.request.OnRequest;
import com.timbacsi.main.MainActivity;
import com.timbacsi.main.R;

public class FragmentChangePass extends Fragment {

	private View mView;
	private static FragmentChangePass fragment = null;
	private IResult callIResult;
	private static String mTitle;

	private EditText mEdtOldPwd;
	private EditText mEdtNewPwd;
	private EditText mEdtReNewPwd;
	
	public static FragmentChangePass newInstance(String title) {
		if (fragment == null)
			fragment = new FragmentChangePass();
	    mTitle=title;
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if (activity instanceof IResult) {
			callIResult = (IResult) activity;
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.layout_doimatkhau, null);

		return mView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		//
		mEdtOldPwd = (EditText) mView.findViewById(R.id.edOldPwd);
		mEdtNewPwd = (EditText) mView.findViewById(R.id.edNewPwd);
		mEdtReNewPwd = (EditText) mView.findViewById(R.id.edReNewPwd);
		//
		Utils.setTitlePage(mView, mTitle);
		callIResult.onDisplay(Utils.TASK_CHILD, "");
		Utils.setOnTouchHideKeyboard(getActivity(),
				(ViewGroup)mView);
		// request change password
		Button btn = (Button) mView.findViewById(R.id.btnSubmit);
		btn.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                requestChangePwd();
            }
        });
	}
	
	private void requestChangePwd() {
	    final String oldPwd = mEdtOldPwd.getText().toString();
	    final String newPwd = mEdtNewPwd.getText().toString();
	    final String reNewPwd = mEdtReNewPwd.getText().toString();
	    if(TextUtils.isEmpty(oldPwd)) {
	        Utils.showMessage(getActivity(), R.string.mat_khau_cu_khong_duoc_de_trong);
	        return;
	    }
	    if(TextUtils.isEmpty(newPwd)) {
            Utils.showMessage(getActivity(), R.string.mat_khau_moi_khong_duoc_de_trong);
            return;
        }
	    if(TextUtils.isEmpty(reNewPwd) || !reNewPwd.equals(newPwd)) {
            Utils.showMessage(getActivity(), R.string.mat_khau_moi_va_nhac_lai_mat_khau_moi_khong_khop);
            return;
        }
	    final UserResponse user = Utils.getUserLogin(getActivity());
	    if(user == null || user.getUser() == null) {
	        Utils.showMessage(getActivity(), R.string.ban_chua_dang_nhap);
	        return;
	    }
	    MainActivity.mLoadingData.setVisibility(View.VISIBLE);
	    Utils.disableEnableControls(false, (ViewGroup)mView);
	    ServerController.getInstance(getActivity()).requestProfileChangePass(user.getUser().getId(), user.getToken(), oldPwd, reNewPwd, new OnRequest<DataItemResponse>() {
            
            @Override
            public void onSuccess(DataItemResponse data) {
            	Utils.disableEnableControls(true, (ViewGroup)mView);
            	MainActivity.mLoadingData.setVisibility(View.GONE);
            	Utils.showMessage(getActivity(), R.string.thay_doi_mat_khau_thanh_cong);
                Utils.requestLogin(getActivity(), callIResult, user.getUser().getEmail(), newPwd, false);
            }
            
            @Override
            public void onError(int errorCode, String message) {
                Utils.showMessage(getActivity(), message);
            }
        });
	}
}
